//
//  CRShowPictureViewController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/28.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//  展示大图片的控制器

#import "CRShowPictureViewController.h"
#import "UIImageView+WebCache.h"
#import "CRTopic.h"
#import "MBProgressHUD+MJ.h"
@interface CRShowPictureViewController () <UIScrollViewDelegate>
/** 图片控件 */
@property (nonatomic, weak) UIImageView *imageView;
@end

@implementation CRShowPictureViewController

#pragma mark >>>> 懒加载

#pragma mark >>>> 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 自定义控件初始化
    [self setupSubViews];
    
    // 初始化数据模型
    [self initData];
}

#pragma mark >>>> 初始化方法
/**
 *  自定义控件初始化
 */
- (void)setupSubViews {
    // 创建一个scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    scrollView.delegate = self;
    // 插入到最前面
    [self.view insertSubview:scrollView atIndex:0];
    
    // imageView
    UIImageView *imageView = [[UIImageView alloc] init];
    // 设置可以交互
    imageView.userInteractionEnabled = YES;
    // 添加手势
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
    [imageView addGestureRecognizer:tapRecognizer];
    [scrollView addSubview:imageView];
    self.imageView = imageView;
    
    // 计算图片的frame
    imageView.x = 0;
    imageView.width = CRScreenW;
    imageView.height = imageView.width * self.topic.height / self.topic.width;
    if (imageView.height > CRScreenH) { // 图片高度 超过 屏幕高度
        imageView.y = 0;
        
        // 设置内容大小
        scrollView.contentSize = CGSizeMake(0, imageView.height);
    } else {
        imageView.y = (CRScreenH - imageView.height) * 0.5;
    }
    
    // 设置缩放比例
    CGFloat scale = self.topic.width / imageView.width;
    if (scale > 1.0) {
        // 最大缩放比
        scrollView.maximumZoomScale = scale;
    }
}


/**
 *  初始化数据模型
 */
- (void)initData {
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.topic.large_image]];
}

#pragma mark >>>> 事件监听方法
- (IBAction)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save {
    // 保存图片的回调方法必须有三个参数 方法名可以自定义
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (IBAction)zhuanFa {
    [UIView animateWithDuration:0.5 animations:^{
        [MBProgressHUD showMessage:@"分享功能暂未实现"];
    } completion:^(BOOL finished) {
        [MBProgressHUD hideHUD];
    }];
}
#pragma mark >>>> 实现代理方法
 //实现缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

#pragma mark >>>> 其他方法
//- (void)abc:(UIImage *)image abc:(NSError *)error abc:(void *)contextInfo
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    if (error) {
        [MBProgressHUD showError:@"保存失败"];
    } else {
        [MBProgressHUD showSuccess:@"保存成功"];
    }
    
}

@end
